using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using Accountability4net;

namespace Accountability4net.FileMonengineProvider
{
    public class FileTransaction: BaseTransaction
    {
        FileMonengineProvider provider_;
        public FileTransaction(FileMonengineProvider provider)
        {
            provider_ = provider;
        }

        public override void Commit()
        {
            try
            {
                base.Commit();
                foreach (IAction action in this)
                {
                    if (action.Type == ActionType.CREDIT)
                    {
                        action.Account.Credit(action.Amount, this);
                    }
                    else if (action.Type == ActionType.DEBIT)
                    {
                        action.Account.Debit(action.Amount, this);
                    }
                }
                provider_.SaveTransaction(this);
                isCommitted_ = true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private DateTimeOffset timeStamp_;
        public override DateTimeOffset Timestamp
        {
            get
            {
                return timeStamp_;
            }
            set
            {
                timeStamp_ = value;
            }
        }

        private string description_;
        public override string Description
        {
            get
            {
                return description_;
            }
            set
            {
                description_ = value;
            }
        }

        private bool isCommitted_;
        public override bool IsCommitted
        {
            get { return isCommitted_; }
        }

        private Guid id_;
        public override Guid ID
        {
            get
            {
                return id_;
            }
            set
            {
                id_ = value;
            }
        }

        public static ITransaction Parse(XmlNode node, FileMonengineProvider provider)
        {
            FileTransaction transaction = new FileTransaction(provider);
            transaction.Timestamp = XmlConvert.ToDateTimeOffset(node.Attributes["timestamp"].Value, "yyyy-MM-ddTHH:mm:ss.fffzzzzzz");
            transaction.Description = provider.Storage.SelectSingleNode(node, "x:description").InnerText;
            XmlNodeList actionNodes = provider.Storage.SelectNodes(node, "x:action");
            foreach (XmlNode actionNode in actionNodes)
            {
                transaction.Add(FileAction.Parse(actionNode, provider));
            }
            return transaction;            
        }

        public XmlNode Write(XmlNode element)
        {
            XmlNode node = element.OwnerDocument.CreateElement("transaction", element.NamespaceURI);
            element.AppendChild(node);
            XmlUtility.CreateAttribute(node, "timestamp", XmlConvert.ToString(Timestamp, "yyyy-MM-ddTHH:mm:ss.fffzzzzzz"));
            XmlUtility.CreateTextElement(node, "description", Description);
            foreach (IAction action in this)
            {
                (action as FileAction).Write(node);
            }
            return node;
        }
    }
}
